package com.nowcoder.community.controller.interceptor;

import com.nowcoder.community.service.DataService;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.controller.interceptor
 * @Author: Master_XXJS
 * @CreateTime: 2023-01-11  23:40
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class DateInterceptor implements HandlerInterceptor {
    @Autowired
    private DataService dataService;
    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //统计 ip，UV独立访客
        String ip = request.getRemoteHost();
        dataService.recordUV(ip);

        //统计 用户，DAU活跃用户
        if(hostHolder.getUser() != null){
            dataService.recordDAU(hostHolder.getUser().getId());
        }

        return true;
    }
}

